package edu.zqu.course.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.course.project.domain.pojo.CourseSyllabusContent;
import edu.zqu.course.project.mapper.CourseSyllabusContentMapper;
import edu.zqu.course.project.service.CourseSyllabusContentService;
import edu.zqu.course.response.ChapterShow;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程大纲明细，即：知识点 服务实现类
 * </p>
 *
 * @author 郭泽鹏
 * @since 2020-06-24
 */
@Service
public class CourseSyllabusContentServiceImpl extends ServiceImpl<CourseSyllabusContentMapper, CourseSyllabusContent> implements CourseSyllabusContentService {
    @Resource
    CourseSyllabusContentMapper courseSyllabusContentMapper;

    @Override
    public List<CourseSyllabusContent> getCourseSyllabusContentList() {
        return courseSyllabusContentMapper.selectList(null);
    }

    @Override
    public List<ChapterShow> getChapterShowById(String id, Integer page, Integer size) {
        //设置查询条件
        QueryWrapper<CourseSyllabusContent> wrapper = new QueryWrapper<>();
        wrapper.eq("course_syllabus_description_id", id);
        //设置分页
        Page page1 = new Page(page, size);
        List<CourseSyllabusContent> courseSyllabusContents = courseSyllabusContentMapper.selectPage(page1, wrapper).getRecords();
        //封装到 ChapterShow 对象中
        List<ChapterShow> chapterShows = new ArrayList<>();
        for (CourseSyllabusContent courseSyllabusContent : courseSyllabusContents) {
            Long csId = courseSyllabusContent.getId();
            Integer session = courseSyllabusContent.getSession();
            String name = courseSyllabusContent.getName();
            Integer teachingHour = courseSyllabusContent.getTeachingHour();
            String content = courseSyllabusContent.getContent();
            ChapterShow chapterShow = new ChapterShow(csId, session, name, teachingHour, content);
            chapterShows.add(chapterShow);
        }
        return chapterShows;
    }

    @Override
    public Integer deleteCSCs() {
        return courseSyllabusContentMapper.delete(null);
    }

    @Override
    public Integer deleteByCSCId(Long id) {
        return  courseSyllabusContentMapper.deleteById(id);
    }

    @Override
    public Integer insertCourseSyllabusContent(CourseSyllabusContent courseSyllabusContent) {
        int result = courseSyllabusContentMapper.insert(courseSyllabusContent);
        return result;
    }
}
